/*封装form表单异步提交*/
$.fn.on_submit = function (callback) {
    if($(this).attr('method') == 'post'){
        var data = new FormData($(this)[0])
    }else{
        $(this).attr('method','get')
        var data = $(this).serialize()
    }
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method').toUpperCase(),
        cache: false,
        data: data,
        processData: false,
        contentType: false,
        dataType: "json",
        beforeSend: function (res) {
        },
        success: function (res) {
            if(callback){callback(res);}
        }
    });
}

/*获取文件的bold对象链接*/
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) {
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) {
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) {
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

/*form表单下面的‘.form_submit’元素点击触发表单异步提交*/
$(function () {
    $('.form_submit').click(function(){
        $(this).parents('form').on_submit();
    });
})


/*需要处理回调数据时，要自定义异步请求*/
//$('#login button').unbind('click');//清除表单的‘.form_submit’元素点击元素点击事件防止请求重复两次提交
//$('#login button').click(function(event){
//    $(this).parents('form').on_submit(function(e){  //在点击事件里面调用父级form的on_submit事件回调接收一个回调参数
//
//    });
//});





function setHeadSearchList(arr) {
    $.ajax({
        url:"/index/search/searchRan",
        success:function(res){
            $('.header .headss .search_list').html('');
            $('.header .headss .search_list').show();
            $('.header .headss .input .host_search').show();
            $('.header .headss .input .select .option').hide();
            console.log(res);
            res.rankings.forEach(function (item) {
                $('.header .headss .search_list').append('<span><b>' + item.text + '</b><i>' + item.num + '条结果</i></span>');
            });
        }
    });
}

function headJumpSearch() {
    if($('.header .headss .input input').val() == '') {
        $('.header .headss .input .tip').html('搜索词不能为空');
        $('.header .headss .input .tip').fadeIn();
        setTimeout(function () {
            $('.header .headss .input .tip').fadeOut(100);
            setTimeout(function(){
                $('.header .headss .input .tip').html('');
            },100);
        }, 1500);
        return;
    }
    
    var opt = {
        value:$('.header .headss .input input').val(),
        type:parseInt($('.header .headss .input .select .value').attr('value'))
    }

    $.ajax({
        url:"/index/search/index",
        type:"post",
        data:opt,
        success:function(res){
            console.log(res.url)
            window.location = res.url;
        }
    });
    console.log(opt);
    
}


/** 把jquery的serializeArray得到的数组转为对象*/
function unserializeArray(data){
    var isdata = {};
    data.forEach(function(item){
        if(isdata[item.name]){
            var arr = [];
            if(typeof(isdata[item.name]) != 'string'){
                isdata[item.name].forEach(function(items){
                    arr.push(items);
                });
                arr.push(item.value);
            }else{
                arr = [isdata[item.name],item.value]
            }
            isdata[item.name] = arr;
        }else{
            isdata[item.name] = item.value
        }
    });
    return isdata;
}